clear all
set more off
capture log close
log using "$output/figure4.log", replace

version 16	

use "$data/ctctaxsim_mar1kid1.dta", clear
append using "$data/ctctaxsim_mar1kid2.dta"
append using "$data/ctctaxsim_mar1kid3.dta"
tab depx

*** and total income
gen totinc2017=negfiitax+earnings if year==2017
bysort earnings depx: egen totinc17=max(totinc2017)
gen totinc2018=negfiitax+earnings if year==2018
bysort earnings depx: egen totinc18=max(totinc2018)
gen totinc2021=negfedtax2021+earnings 

gen diftotinc20212018=totinc2021-totinc18
gen diftotinc20212017=totinc2021-totinc17
gen diftotinc20182017=totinc18-totinc17

sum totinc* diftotinc*

*** to see where tax credits fall to zero
gen min2017=totinc17-earnings
gen min2018=totinc18-earnings
gen min2021=totinc2021-earnings
tab earnings min2017 if depx==1 & abs(min2017)<.005
tab earnings min2018 if depx==1 & abs(min2018)<.005
tab earnings min2021 if depx==1 & abs(min2021)<.005

foreach var in totinc17 earnings totinc18 totinc2021{
	replace `var'=`var'/10
}

twoway line diftotinc20212018 earnings if year==2018 & depx==1, pstyle(p1) || line diftotinc20212017 earnings if year==2017 & depx==1, lp(_) pstyle(p2) || line diftotinc20182017 earnings if year==2018 & depx==1, lp(-)  pstyle(p1) graphregion(color(white)) bgcolor(white) leg(order(1 "2021 vs 2020" 2 "2021 vs 2017" 3 "2020 vs 2017") c(1) pos(12) ring(0)) ytitle("Change in Tax Credits ($1,000s)") xtitle("Pre-Tax Earnings ($10,000s)" "{bf:Panel A: One Child}") ylabel(0(3.000)9.000)
graph save "$output/changeinaftertaxincome1.gph", replace

twoway line diftotinc20212018 earnings if year==2018 & depx==2, pstyle(p1) || line diftotinc20212017 earnings if year==2017 & depx==2, lp(_) pstyle(p2) || line diftotinc20182017 earnings if year==2018 & depx==2, lp(-)  pstyle(p1) graphregion(color(white)) bgcolor(white) leg(order(1 "2021 vs 2020" 2 "2021 vs 2017" 3 "2020 vs 2017") c(1) pos(12) ring(0)) ytitle("Change in Tax Credits ($1,000s)") xtitle("Pre-Tax Earnings ($10,000s)" "{bf:Panel B: Two Children}") ylabel(0(3.000)9.000)
graph save "$output/changeinaftertaxincome2.gph", replace


twoway line diftotinc20212018 earnings if year==2018 & depx==3, pstyle(p1) || line diftotinc20212017 earnings if year==2017 & depx==3, lp(_) pstyle(p2) || line diftotinc20182017 earnings if year==2018 & depx==3, lp(-)  pstyle(p1) graphregion(color(white)) bgcolor(white) leg(off) text(8 6 "See legend in other panels" , box fc(white) margin(small) j(left) size(medsmall)) ytitle("Change in Tax Credits ($1,000s)") xtitle("Pre-Tax Earnings ($10,000s)" "{bf:Panel C: Three Children}") ylabel(0(3.000)9.000)
graph save "$output/changeinaftertaxincome3.gph", replace

graph combine "$output/changeinaftertaxincome1.gph" "$output/changeinaftertaxincome2.gph" "$output/changeinaftertaxincome3.gph",graphregion(color(white)) 
graph export "$output/figure4.png", replace

erase "$output/changeinaftertaxincome1.gph" 
erase "$output/changeinaftertaxincome2.gph" 
erase "$output/changeinaftertaxincome3.gph"

log close